﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.templates;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class PetItemTable
    {
        private static Logger _log = Logger.getLogger( typeof( PetItemTable ).FullName );

        private static PetItemTable _instance;

        private Dictionary<int, L1PetItem> _petItemIdIndex = new Dictionary<int, L1PetItem>();

        public static PetItemTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new PetItemTable();
            }
            return _instance;
        }

        private PetItemTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM petitem";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1PetItem petItem = new L1PetItem();
                    petItem.setItemId( SqlUtils.toInt( item["item_id"] ) );
                    petItem.setHitModifier( SqlUtils.toInt( item["hitmodifier"] ) );
                    petItem.setDamageModifier( SqlUtils.toInt( item["dmgmodifier"] ) );
                    petItem.setAddAc( SqlUtils.toInt( item["ac"] ) );
                    petItem.setAddStr( SqlUtils.toInt( item["add_str"] ) );
                    petItem.setAddCon( SqlUtils.toInt( item["add_con"] ) );
                    petItem.setAddDex( SqlUtils.toInt( item["add_dex"] ) );
                    petItem.setAddInt( SqlUtils.toInt( item["add_int"] ) );
                    petItem.setAddWis( SqlUtils.toInt( item["add_wis"] ) );
                    petItem.setAddHp( SqlUtils.toInt( item["add_hp"] ) );
                    petItem.setAddMp( SqlUtils.toInt( item["add_mp"] ) );
                    petItem.setAddSp( SqlUtils.toInt( item["add_sp"] ) );
                    petItem.setAddMr( SqlUtils.toInt( item["m_def"] ) );
                    _petItemIdIndex[petItem.getItemId()] = petItem;
                }
            }
        }

        public L1PetItem getTemplate( int itemId )
        {/*TTOK*/
            L1PetItem ret;
            _petItemIdIndex.TryGetValue( itemId, out ret );
            return ret;
        }
    }
}
